<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\Admin\AdminController;
class GoodsRecomController extends Controller
{
    public function getAdd(){
      //将所有的商品列出来以便选择添加到推荐列表
        $goodsact = DB::table('tbl_goods')->select('id','goodsname')->get();
        return view('admin.goodsrecom.add',['goodsact'=>$goodsact]);
   }



     /**
      * 推荐商品添加控制方法
      * @param  Request $request [description]
      * @return [type]           [description]
      */
    public function postDoadd(Request $request)
    {
      // dd($request->all());
       $data = $request -> all();
       
       //先进行上传图片的处理 缩放 160*160
       if(!empty($data['recompic'])){
       
        //获取上传文件的后缀
        $ext = $request -> file('recompic') -> getClientOriginalExtension();
        //随机生成一个文件前缀名字
        $filename = date('Ymd',time()).mt_rand(10000,99999);
        //前缀后缀拼接成最后新的文件名称
        $newfilename = $filename.'.'.$ext;
        //上传文件到指定目录
        $request -> file('recompic') -> move('home_assoc/images/',$newfilename);
        //获取上传文件的长宽
        $arr = getimagesize('home_assoc/images/'.$newfilename);
        $width = $arr[0];
        $height = $arr[1];
        //根据指定大小来进行缩放处理  调用的admin控制器里面的方法
        AdminController::suofang('home_assoc/images/'.$newfilename,120,120,$pre = 'recompic');
        //缩放完成后删除上传的原图片
        unlink('home_assoc/images/'.$newfilename);
        //最后需要存储到数据库的图片名字
        $recompic = 'recompic'.$newfilename;

       }else{
            $recompic = 'default120.jpg';
       }
       $data = $request -> except('_token');
       $data['recompic'] = $recompic;
       // 查询商品在商品表的信息
        $recomname = DB::table('tbl_goods') -> where('id',$data['recomname']) -> first();
        // 将商品的信息关联商品推荐
       $data['recomname'] = $recomname['goodsname'];
       $data['gid'] = $recomname['id'];
              // dd($data);
       //验证必填选项
       $this->validate($request, [
        'recomname' => 'required',
        'recompic' => 'required',
        'recomdesc' => 'required',
        'recomprice' => 'required',
        ],[
        'recomname.required'=>'商品名称不能为空',
        'recompic.required'=>'商品展示图片不能为空',
        'recomdesc.required'=>'商品描述不能为空',
        'recomprice.required'=>'商品价格不能为空',

        ]);
        //将需要的数据插入数据库
        $res = DB::table('tbl_goodsrecom') -> insert(
            ['recomname'=>$data['recomname'],
            'recompic'=>$data['recompic'],
            'recomdesc'=>$data['recomdesc'],
            'recomprice'=>$data['recomprice'],
            'gid'=>$data['gid'],
            ]
        );
        $res1 = DB::table('tbl_goods') -> where('id',$data['gid']) -> update(['goodsprice'=>$data['recomprice']]);
       //进行判断 如果插入成功则进入到列表查看；如果失败则回到之前界面
       if($res){
            return redirect('/admin/goodsrecom/list')->with('success','添加成功');
       }else{
            return back()->with('error','添加失败');
       }
    }

        /**
         *  推荐商品列表控制
         */
   public function getList(Request $request){
        $count = $request ->input('count',10);
        $search = $request->input('search','');
        // 分页搜索功能
        $data = DB::table('tbl_goodsrecom') -> where('recomname','like','%'.$search.'%') ->paginate($count);
        return view('admin.goodsrecom.list',['data'=>$data,'request'=>$request->all()]);
   }

     /**
      * 推荐商品删除功能控制
      */
    public function getDelete($id){
         $res = DB::table('tbl_goodsrecom')->where('id',$id)->delete();
         if($res){
             return redirect('/admin/goodsrecom/list')->with('success','删除成功');
         }else{
             return redirect('/admin/goodsrecom/list')->with('error','删除失败');
         }
    }
    public function getEdit($id){
      //将所有的商品列出来以便选择添加到活动列表
        
       $data = DB::table('tbl_goodsrecom') -> where('id',$id)->first();
       
       return view('admin.goodsrecom.update',['data'=>$data]);
    }


        /**
         *  修改信息更改控制
         */
    public function postUpdate(Request $request)
    {

        $data = $request -> all();
       //先判断图片是否上传 进行上传图片的处理 缩放 160*160
       if(!empty($data['recompic'])){
            //获取上传文件的后缀
            $ext = $request -> file('recompic') -> getClientOriginalExtension();
            //随机生成一个文件前缀名字
            $filename = date('Ymd',time()).mt_rand(10000,99999);
            //前缀后缀拼接成最后新的文件名称
            $newfilename = $filename.'.'.$ext;
            //上传文件到指定目录
            $request -> file('recompic') -> move('home_assoc/images/',$newfilename);
            //获取上传文件的长宽
            $arr = getimagesize('home_assoc/images/'.$newfilename);
            $width = $arr[0];
            $height = $arr[1];
            //根据指定大小来进行缩放处理
            AdminController::suofang('home_assoc/images/'.$newfilename,120,120,$pre = 'recompic');
            //缩放完成后删除上传的原图片
            unlink('home_assoc/images/'.$newfilename);
            //最后需要存储到数据库的图片名字
            $recompic = 'recompic'.$newfilename;
           }else{
            //将为对应的数据库图片查询出来
                $s = DB::table('tbl_goodsrecom')
                            ->where('id',$data['id'])
                            ->first();
                $recompic = $s['recompic'];
           }
           $data['recompic'] = $recompic;
           // dd($data);
           //将接受到的的数据修改进数据库
           $res = DB::table('tbl_goodsrecom')
                ->where('id',$data['id'])
                ->update(
                        [
                            'recomname'=>$data['recomname'],
                            'recompic'=>$data['recompic'],
                            'recomdesc'=>$data['recomdesc'],
                            'recomprice'=>$data['recomprice'],
                           
                        ]
                    );
          $res1 = DB::table('tbl_goods') -> where('id',$data['gid']) -> update(['goodsprice'=>$data['recomprice']]);
       //进行判断 如果插入成功则进入到列表查看；如果失败则回到之前界面
       if($res){
            return redirect('/admin/goodsrecom/list')->with('success','更新成功');
       }else{
            return back()->with('error','更新失败');
       }
    }

}
